<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="title">
      <el-input v-model="dataForm.title" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="service type(6 kinds)" prop="type">
      <el-input v-model="dataForm.type" placeholder="service type(6 kinds)"></el-input>
    </el-form-item>
    <el-form-item label="tag" prop="tags">
      <el-input v-model="dataForm.tags" placeholder="tag"></el-input>
    </el-form-item>
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="url">
      <el-input v-model="dataForm.url" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="healthcheckUrl">
      <el-input v-model="dataForm.healthcheckUrl" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="img">
      <el-input v-model="dataForm.img" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="repository">
      <el-input v-model="dataForm.repository" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="license">
      <el-input v-model="dataForm.license" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="description">
      <el-input v-model="dataForm.description" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="short" prop="shortDesc">
      <el-input v-model="dataForm.shortDesc" placeholder="short"></el-input>
    </el-form-item>
    <el-form-item label="" prop="date">
      <el-input v-model="dataForm.date" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dockerImage">
      <el-input v-model="dataForm.dockerImage" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="version">
      <el-input v-model="dataForm.version" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="originalVersionId">
      <el-input v-model="dataForm.originalVersionId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="inputFormat">
      <el-input v-model="dataForm.inputFormat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="outputFormat">
      <el-input v-model="dataForm.outputFormat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="dockerUrl">
      <el-input v-model="dataForm.dockerUrl" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="checkflag">
      <el-input v-model="dataForm.checkflag" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="paper">
      <el-input v-model="dataForm.paper" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          type: '',
          tags: '',
          userId: '',
          url: '',
          healthcheckUrl: '',
          img: '',
          repository: '',
          license: '',
          description: '',
          shortDesc: '',
          date: '',
          dockerImage: '',
          version: '',
          originalVersionId: '',
          inputFormat: '',
          outputFormat: '',
          dockerUrl: '',
          checkflag: '',
          paper: ''
        },
        dataRule: {
          title: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: 'service type(6 kinds)不能为空', trigger: 'blur' }
          ],
          tags: [
            { required: true, message: 'tag不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          healthcheckUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          repository: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          license: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          shortDesc: [
            { required: true, message: 'short不能为空', trigger: 'blur' }
          ],
          date: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dockerImage: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          originalVersionId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          inputFormat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          outputFormat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dockerUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          checkflag: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          paper: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/service/infor/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.infor.title
                this.dataForm.type = data.infor.type
                this.dataForm.tags = data.infor.tags
                this.dataForm.userId = data.infor.userId
                this.dataForm.url = data.infor.url
                this.dataForm.healthcheckUrl = data.infor.healthcheckUrl
                this.dataForm.img = data.infor.img
                this.dataForm.repository = data.infor.repository
                this.dataForm.license = data.infor.license
                this.dataForm.description = data.infor.description
                this.dataForm.shortDesc = data.infor.shortDesc
                this.dataForm.date = data.infor.date
                this.dataForm.dockerImage = data.infor.dockerImage
                this.dataForm.version = data.infor.version
                this.dataForm.originalVersionId = data.infor.originalVersionId
                this.dataForm.inputFormat = data.infor.inputFormat
                this.dataForm.outputFormat = data.infor.outputFormat
                this.dataForm.dockerUrl = data.infor.dockerUrl
                this.dataForm.checkflag = data.infor.checkflag
                this.dataForm.paper = data.infor.paper
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/service/infor/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'type': this.dataForm.type,
                'tags': this.dataForm.tags,
                'userId': this.dataForm.userId,
                'url': this.dataForm.url,
                'healthcheckUrl': this.dataForm.healthcheckUrl,
                'img': this.dataForm.img,
                'repository': this.dataForm.repository,
                'license': this.dataForm.license,
                'description': this.dataForm.description,
                'shortDesc': this.dataForm.shortDesc,
                'date': this.dataForm.date,
                'dockerImage': this.dataForm.dockerImage,
                'version': this.dataForm.version,
                'originalVersionId': this.dataForm.originalVersionId,
                'inputFormat': this.dataForm.inputFormat,
                'outputFormat': this.dataForm.outputFormat,
                'dockerUrl': this.dataForm.dockerUrl,
                'checkflag': this.dataForm.checkflag,
                'paper': this.dataForm.paper
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
